package vikings.aps;

import javax.servlet.http.HttpServlet;

/**
 * @author Administrator
 *
 */


import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DBman.DatabaseManager;

import vikings.encrypter.Encrypt;




@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet implements Servlet
    {
  	
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
        	
        	Encrypt encrypt = new Encrypt();
      
            String usr = request.getParameter("username");
            String password = request.getParameter("password");
            encrypt.setPassword(password);
            
            StatementBean statementBean = new StatementBean();
            


    		DatabaseManager dbm;
    		

    		try {
				dbm = new DatabaseManager();
				String psd = dbm.retrPassword(usr);
				
	        	
	            if(encrypt.getPassword().equals(psd))
	             {
	            	password = encrypt.getPassword();
	         	    getServletContext().getRequestDispatcher("/welcome.jsp").forward(request, response);
	         	    //System.out.print(password);
	              }else
	              {
	            	  //String message = "authentication falied.";
	            	  //request.getSession().setAttribute("login_failed", message);
	            	  getServletContext().getRequestDispatcher("/loginError.jsp").forward(request, response);
				
                }
				
			} catch (ClassNotFoundException e) {
				//String message = "authentication falied.";
          	  	//request.getSession().setAttribute("login_failed", message);
          	  	getServletContext().getRequestDispatcher("/loginError.jsp").forward(request, response);
				// TODO Auto-generated catch block
				//e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				//String message = "authentication falied.";
          	  	//request.getSession().setAttribute("login_failed", message);
          	  	getServletContext().getRequestDispatcher("/loginError.jsp").forward(request, response);
				//e.printStackTrace();
			}
    		
                             
      }
       	

        

        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
        {
            doPost(request,response);
        }
}

